Snarf the deletion log entries on the undelete page. Make the log items
[lhc/web/wiklou.git] / includes / SpecialLog.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 function wfSpecialLog( $par = '' ) {
21 global $wgRequest;
22 $logReader =& new LogReader( $wgRequest );
23 if( '' == $wgRequest->getVal( 'type' ) && !empty( $par ) ) {
24 $logReader->limitType( $par );
25 }
26 $logViewer =& new LogViewer( $logReader );
27 $logViewer->show();
28 $logReader->cleanUp();
29 }
30
31 class LogReader {
32 var $db, $joinClauses, $whereClauses;
33 var $type = '', $user = '', $title = null;
34
35 function LogReader( $request ) {
36 $this->db =& wfGetDB( DB_SLAVE );
37 $this->setupQuery( $request );
38 }
39
40 function setupQuery( $request ) {
41 $cur = $this->db->tableName( 'cur' );
42 $user = $this->db->tableName( 'user' );
43 $this->joinClauses = array( "LEFT OUTER JOIN $cur ON log_namespace=cur_namespace AND log_title=cur_title" );
44 $this->whereClauses = array( 'user_id=log_user' );
45
46 $this->limitType( $request->getVal( 'type' ) );
47 $this->limitUser( $request->getText( 'user' ) );
48 $this->limitTitle( $request->getText( 'page' ) );
49 $this->limitTime( $request->getVal( 'from' ), '>=' );
50 $this->limitTime( $request->getVal( 'until' ), '<=' );
51
52 list( $this->limit, $this->offset ) = $request->getLimitOffset();
53 }
54
55 function limitType( $type ) {
56 if( empty( $type ) ) {
57 return false;
58 }
59 $this->type = $type;
60 $safetype = $this->db->strencode( $type );
61 $this->whereClauses[] = "log_type='$safetype'";
62 }
63
64 function limitUser( $name ) {
65 $title = Title::makeTitleSafe( NS_USER, $name );
66 if( empty( $name ) || is_null( $title ) ) {
67 return false;
68 }
69 $this->user = str_replace( '_', ' ', $title->getDBkey() );
70 $safename = $this->db->strencode( $this->user );
71 $user = $this->db->tableName( 'user' );
72 $this->whereClauses[] = "user_name='$safename'";
73 }
74
75 function limitTitle( $page ) {
76 $title = Title::newFromText( $page );
77 if( empty( $page ) || is_null( $title ) ) {
78 return false;
79 }
80 $this->title =& $title;
81 $safetitle = $this->db->strencode( $title->getDBkey() );
82 $ns = $title->getNamespace();
83 $this->whereClauses[] = "log_namespace=$ns AND log_title='$safetitle'";
84 }
85
86 function limitTime( $time, $direction ) {
87 # Direction should be a comparison operator
88 if( empty( $time ) ) {
89 return false;
90 }
91 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
92 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
93 }
94
95 function getQuery() {
96 $logging = $this->db->tableName( "logging" );
97 $user = $this->db->tableName( 'user' );
98 $sql = "SELECT log_type, log_action, log_timestamp,
99 log_user, user_name,
100 log_namespace, log_title, cur_id,
101 log_comment FROM $logging, $user ";
102 if( !empty( $this->joinClauses ) ) {
103 $sql .= implode( ',', $this->joinClauses );
104 }
105 if( !empty( $this->whereClauses ) ) {
106 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
107 }
108 $sql .= " ORDER BY log_timestamp DESC ";
109 $sql .= $this->db->limitResult( $this->limit, $this->offset );
110 return $sql;
111 }
112
113 function initQuery() {
114 $this->result = $this->db->query( $this->getQuery() );
115 }
116
117 function fetchObject() {
118 return $this->db->fetchObject( $this->result );
119 }
120
121 function cleanUp() {
122 $this->db->freeResult( $this->result );
123 }
124
125 function queryType() {
126 return $this->type;
127 }
128
129 function queryUser() {
130 return $this->user;
131 }
132
133 function queryTitle() {
134 if( is_null( $this->title ) ) {
135 return '';
136 } else {
137 return $this->title->getPrefixedText();
138 }
139 }
140 }
141
142 class LogViewer {
143 var $reader, $skin;
144
145 function LogViewer( &$reader ) {
146 global $wgUser;
147 $this->skin =& $wgUser->getSkin();
148 $this->reader =& $reader;
149 }
150
151 function show() {
152 global $wgOut;
153 $this->showHeader( $wgOut );
154 $this->showOptions( $wgOut );
155 $this->showPrevNext( $wgOut );
156 $this->showList( $wgOut );
157 $this->showPrevNext( $wgOut );
158 }
159
160 function showList( &$out ) {
161 $html = "";
162 $this->reader->initQuery();
163 while( $s = $this->reader->fetchObject() ) {
164 $html .= $this->logLine( $s );
165 }
166 $out->addHTML( $html );
167 }
168
169 # wfMsg( unprotectedarticle, $text )
170 # wfMsg( 'protectedarticle', $text )
171 # wfMsg( 'deletedarticle', $text )
172 # wfMsg( 'uploadedimage', $text )
173 # wfMsg( "blocklogentry", $this->BlockAddress, $this->BlockExpiry );
174 # wfMsg( "bureaucratlogentry", $this->mUser, implode( " ", $rightsNotation ) );
175 # wfMsg( "undeletedarticle", $this->mTarget ),
176 function logLine( $s ) {
177 global $wgLang;
178 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
179 $user = Title::makeTitleSafe( NS_USER, $s->user_name );
180 $time = $wgLang->timeanddate( $s->log_timestamp );
181 if( $s->cur_id ) {
182 $titleLink = $this->skin->makeKnownLinkObj( $title );
183 } else {
184 $titleLink = $this->skin->makeBrokenLinkObj( $title );
185 }
186 $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
187 if( '' === $s->log_comment ) {
188 $comment = '';
189 } else {
190 $comment = '(<em>' . $this->skin->formatComment( $s->log_comment ) . '</em>)';
191 }
192
193 $action = LogPage::actionText( $s->log_type, $s->log_action, $titleLink );
194 $out = "<li>$time $userLink $action $comment</li>\n";
195 return $out;
196 }
197
198 function showHeader( &$out ) {
199 $type = $this->reader->queryType();
200 if( LogPage::isLogType( $type ) ) {
201 $out->setPageTitle( LogPage::logName( $type ) );
202 $out->addWikiText( LogPage::logHeader( $type ) );
203 }
204 }
205
206 function showOptions( &$out ) {
207 global $wgScript;
208 $action = htmlspecialchars( $wgScript );
209 $title = Title::makeTitle( NS_SPECIAL, 'Log' );
210 $special = htmlspecialchars( $title->getPrefixedDBkey() );
211 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
212 "<input type='hidden' name='title' value=\"$special\" />\n" .
213 $this->getTypeMenu() .
214 $this->getUserInput() .
215 $this->getTitleInput() .
216 "<input type='submit' />" .
217 "</form>" );
218 }
219
220 function getTypeMenu() {
221 $out = "<select name='type'>\n";
222 foreach( LogPage::validTypes() as $type ) {
223 $text = htmlspecialchars( LogPage::logName( $type ) );
224 $selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
225 $out .= "<option value=\"$type\"$selected>$text</option>\n";
226 }
227 $out .= "</select>\n";
228 return $out;
229 }
230
231 function getUserInput() {
232 $user = htmlspecialchars( $this->reader->queryUser() );
233 return "User: <input type='text' name='user' size='12' value=\"$user\" />\n";
234 }
235
236 function getTitleInput() {
237 $title = htmlspecialchars( $this->reader->queryTitle() );
238 return "Title: <input type='text' name='page' size='20' value=\"$title\" />\n";
239 }
240
241 function showPrevNext( &$out ) {
242 global $wgLang;
243 $pieces = array();
244 $pieces[] = 'type=' . htmlspecialchars( $this->reader->queryType() );
245 $pieces[] = 'user=' . htmlspecialchars( $this->reader->queryUser() );
246 $pieces[] = 'page=' . htmlspecialchars( $this->reader->queryTitle() );
247 $bits = implode( '&', $pieces );
248 $offset = 0; $limit = 50;
249
250 # TODO: use timestamps instead of offsets to make it more natural
251 # to go huge distances in time
252 $html = wfViewPrevNext( $offset, $limit,
253 $wgLang->specialpage( 'Log' ),
254 $bits,
255 false);
256 $out->addHTML( '<p>' . $html . '</p>' );
257 }
258 }
259
260
261 ?>